﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Classes {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.utils.*;
    import com.adobe.net.*;
    import flash.system.*;
    import flash.net.*;

    public class ResourceLoader implements IEventDispatcher {

        private static const TIMEOUT_LENGTH:Number = 15000;

        private var m_retryCount:int = 0;
        protected var m_url:String = null;
        private var m_zaspLoadHandle:int = 0;
        private var m_domain:String = null;
        private var m_priority:int = 0;
        protected var m_loader:EventDispatcher = null;
        protected var m_completeCallback:Function = null;
        private var m_faultCallback:Function = null;
        protected var m_urlRequest:URLRequest = null;
        private var m_context:LoaderContext = null;
        private var m_dispatcher:EventDispatcher = null;
        private var m_dispatchedEvent:Boolean = false;
        private var m_timeoutTimer:Timer;
        private var m_receivedBytes:Boolean = false;

        public function ResourceLoader(_arg1:String, _arg2:int=0, _arg3:Function=null, _arg4:Function=null){
            var url:* = _arg1;
            var priority:int = _arg2;
            var completeCallback = _arg3;
            var faultCallback = _arg4;
            super();
            var uri:* = new URI(url);
            this.m_dispatcher = new EventDispatcher(this);
            this.m_domain = uri.authority;
            this.m_url = url;
            this.m_priority = priority;
            this.m_completeCallback = completeCallback;
            this.m_faultCallback = faultCallback;
            if (GlobalEngine.zaspManager){
                this.m_zaspLoadHandle = GlobalEngine.zaspManager.zaspWaitStart(((priority == LoadingManager.PRIORITY_HIGH)) ? "HIPRI_ASSET" : "ASSET", this.m_url);
            };
            this.makeLoader();
            this.addEventListeners();
            try {
                Security.allowDomain("*");
            } catch(error:SecurityError) {
            };
            this.m_context = new LoaderContext();
            if (Security.sandboxType == Security.REMOTE){
                this.m_context.securityDomain = SecurityDomain.currentDomain;
            };
            this.m_context.applicationDomain = ApplicationDomain.currentDomain;
            this.m_context.checkPolicyFile = true;
            this.m_urlRequest = new URLRequest(url);
        }
        public function overridePolicy():void{
            this.m_context = new LoaderContext();
            this.m_context.checkPolicyFile = false;
        }
        protected function makeLoader():void{
            var _local1:Loader = new Loader();
            this.m_loader = _local1;
        }
        public function getLoader():EventDispatcher{
            return (this.m_loader);
        }
        public function getPriority():Number{
            return (this.m_priority);
        }
        public function getDomain():String{
            return (this.m_domain);
        }
        public function getURL():String{
            return (this.m_url);
        }
        public function isPackedResource():Boolean{
            return (false);
        }
        public function getbytesTotal():int{
            return ((this.m_loader as Loader).contentLoaderInfo.bytesTotal);
        }
        public function getEventDispatcher():EventDispatcher{
            return ((this.m_loader as Loader).contentLoaderInfo);
        }
        public function startLoad():void{
            try {
                this.chooseLoad();
                this.m_timeoutTimer = new Timer(TIMEOUT_LENGTH);
                this.m_timeoutTimer.addEventListener(TimerEvent.TIMER, this.onTimeout);
                this.m_timeoutTimer.start();
                if (((GlobalEngine.zaspManager) && (!(this.m_zaspLoadHandle)))){
                    this.m_zaspLoadHandle = GlobalEngine.zaspManager.zaspWaitStart("ASSET", this.m_url);
                };
            } catch(error:Error) {
                GlobalEngine.log("Loader", ((("Start load error: " + m_url) + " : ") + error.toString()));
                dispatchFault(new IOErrorEvent(IOErrorEvent.IO_ERROR, false, false, "ResourceLoader Dispatched IOError Event"), ErrorManager.ERROR_FAILED_TO_LOAD);
            };
        }
        protected function chooseLoad():void{
            (this.m_loader as Loader).load(this.m_urlRequest, this.m_context);
        }
        protected function chooseClose():void{
            (this.m_loader as Loader).close();
        }
        public function stopLoad():void{
            this.stopTimeoutTimer();
            if (this.m_loader != null){
                this.removeEventListeners();
                this.m_dispatchedEvent = true;
                try {
                    this.chooseClose();
                } catch(error:Error) {
                };
            };
        }
        protected function get retryCount():int{
            return (this.m_retryCount);
        }
        protected function set retryCount(_arg1:int):void{
            this.m_retryCount = _arg1;
        }
        private function onOpen(_arg1:Event):void{
            GlobalEngine.log("Loader", ("Start load: " + this.m_url));
            this.stopTimeoutTimer();
        }
        private function stopTimeoutTimer():void{
            if (this.m_zaspLoadHandle > 0){
                GlobalEngine.zaspManager.zaspWaitEnd(this.m_zaspLoadHandle);
                this.m_zaspLoadHandle = 0;
            };
            if (this.m_timeoutTimer){
                this.m_timeoutTimer.stop();
                this.m_timeoutTimer.removeEventListener(TimerEvent.TIMER, this.onTimeout);
                this.m_timeoutTimer = null;
            };
        }
        private function onTimeout(_arg1:Event):void{
            this.stopTimeoutTimer();
            if (this.m_dispatchedEvent == false){
                GlobalEngine.log("Loader", ((("Load timed out (try: " + this.retryCount) + "): ") + this.m_url));
            };
            if (this.retryCount >= LoaderConstants.MAX_RETRIES){
                this.dispatchFault(_arg1, ErrorManager.ERROR_LOAD_TIMED_OUT);
            } else {
                this.retryCount++;
                this.startLoad();
            };
        }
        private function onComplete(_arg1:Event):void{
            this.stopTimeoutTimer();
            if (this.m_dispatchedEvent == false){
                this.dispatchEvent(new Event(Event.COMPLETE));
                if (this.m_completeCallback != null){
                    this.invokeCallback(_arg1);
                    this.m_completeCallback = null;
                };
                this.m_dispatchedEvent = true;
                this.removeEventListeners();
            };
        }
        protected function invokeCallback(_arg1:Event):void{
            this.m_completeCallback(_arg1);
        }
        private function onProgress(_arg1:ProgressEvent):void{
            this.m_receivedBytes = true;
            this.stopTimeoutTimer();
        }
        private function dispatchFault(_arg1:Event, _arg2:int, _arg3:Object=null):void{
            this.stopTimeoutTimer();
            if (this.m_dispatchedEvent == false){
                this.dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR, false, false, "ResourceLoader Dispatched IOError Event"));
                this.logError(this.m_url, _arg2, _arg3);
                if (((!((this.m_faultCallback == null))) && ((_arg1 is IOErrorEvent)))){
                    this.m_faultCallback(_arg1);
                };
                this.m_dispatchedEvent = true;
            };
        }
        protected function logError(_arg1:String, _arg2:int, _arg3:Object):void{
            ErrorManager.addError(_arg1, _arg2, _arg3);
        }
        protected function onError(_arg1:IOErrorEvent):void{
            this.stopTimeoutTimer();
            if (this.m_dispatchedEvent == false){
                GlobalEngine.log("Loader", ((("Load error (try: " + this.retryCount) + "): ") + this.m_url));
            };
            if (this.retryCount >= LoaderConstants.MAX_RETRIES){
                this.dispatchFault(_arg1, ErrorManager.ERROR_LOAD_IO_ERROR);
            } else {
                this.retryCount++;
                this.startLoad();
            };
        }
        protected function onSecurityError(_arg1:SecurityErrorEvent):void{
            if (this.m_dispatchedEvent == false){
                this.stopTimeoutTimer();
                if (this.m_dispatchedEvent == false){
                    GlobalEngine.log("Loader", ((("Security Load error (try: " + this.retryCount) + "): ") + this.m_url));
                };
                if (this.retryCount >= LoaderConstants.MAX_RETRIES){
                    this.dispatchFault(_arg1, ErrorManager.ERROR_LOAD_SECURITY_ERROR);
                } else {
                    this.retryCount++;
                    this.startLoad();
                };
            };
        }
        protected function onHTTPStatus(_arg1:HTTPStatusEvent):void{
            if ((((((_arg1.status > 0)) && (!((_arg1.status == 200))))) && ((this.m_dispatchedEvent == false)))){
                if (this.m_dispatchedEvent == false){
                    GlobalEngine.log("Loader", ((((("HTTP Status error (code: " + _arg1.status) + ", try: ") + this.retryCount) + "): ") + this.m_url));
                };
                this.dispatchFault(_arg1, ErrorManager.ERROR_LOAD_HTTP_ERROR, {status:_arg1.status});
            };
        }
        public function addEventListener(_arg1:String, _arg2:Function, _arg3:Boolean=false, _arg4:int=0, _arg5:Boolean=false):void{
            this.m_dispatcher.addEventListener(_arg1, _arg2, _arg3, _arg4);
        }
        public function dispatchEvent(_arg1:Event):Boolean{
            return (this.m_dispatcher.dispatchEvent(_arg1));
        }
        public function hasEventListener(_arg1:String):Boolean{
            return (this.m_dispatcher.hasEventListener(_arg1));
        }
        public function removeEventListener(_arg1:String, _arg2:Function, _arg3:Boolean=false):void{
            this.m_dispatcher.removeEventListener(_arg1, _arg2, _arg3);
        }
        public function willTrigger(_arg1:String):Boolean{
            return (this.m_dispatcher.willTrigger(_arg1));
        }
        protected function addEventListeners():void{
            var _local1:EventDispatcher = this.getEventDispatcher();
            _local1.addEventListener(HTTPStatusEvent.HTTP_STATUS, this.onHTTPStatus);
            _local1.addEventListener(Event.COMPLETE, this.onComplete);
            _local1.addEventListener(IOErrorEvent.IO_ERROR, this.onError);
            _local1.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onSecurityError);
            _local1.addEventListener(ProgressEvent.PROGRESS, this.onProgress);
            _local1.addEventListener(Event.OPEN, this.onOpen);
        }
        protected function removeEventListeners():void{
            var _local1:EventDispatcher = this.getEventDispatcher();
            _local1.removeEventListener(HTTPStatusEvent.HTTP_STATUS, this.onHTTPStatus);
            _local1.removeEventListener(Event.COMPLETE, this.onComplete);
            _local1.removeEventListener(IOErrorEvent.IO_ERROR, this.onError);
            _local1.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onSecurityError);
            _local1.removeEventListener(ProgressEvent.PROGRESS, this.onProgress);
            _local1.removeEventListener(Event.OPEN, this.onOpen);
        }

    }
}//package Engine.Classes 
